package org.example.travel_web.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.login.ForgetReq;
import org.example.travel_web.dto.login.LoginReq;
import org.example.travel_web.dto.login.LoginResult;
import org.example.travel_web.dto.login.RegisterReq;
import org.example.travel_web.dto.user.PageReq;
import org.example.travel_web.dto.user.PageResp;
import org.example.travel_web.dto.user.UserReq;
import org.example.travel_web.entity.UserET;
import org.example.travel_web.exception.AuthException;
import org.example.travel_web.mapper.UserMapper;
import org.example.travel_web.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23591
 * @description 针对表【travel_user(用户表)】的数据库操作Service实现
 * @createDate 2025-01-21 15:58:21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserET>
        implements UserService {

    @Override
    public LoginResult login(LoginReq req) {
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserET::getUsername, req.getUsername());
        UserET userET = getOne(queryWrapper);
        if (userET == null || !userET.getPassword().equals(req.getPassword())) {
            throw new AuthException("用户名或密码错误");
        }
        StpUtil.login(userET.getUid());
        return LoginResult.builder()
                .token(StpUtil.getTokenValue())
                .build();
    }

    @Override
    public void register(RegisterReq req) {
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserET::getUsername, req.getUsername());
        if (exists(queryWrapper)) {
            throw new AuthException("账号已存在");
        }
        UserET userET = BeanUtil.toBean(req, UserET.class);
        if (!save(userET)) {
            throw new AuthException("注册失败");
        }
    }

    @Override
    public PageResult<PageResp> pageResult(PageReq req) {
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(req.getKeyword())) {
            queryWrapper.like(UserET::getNickname, req.getKeyword());
        }
        queryWrapper.orderByDesc(UserET::getUid);
        Page<UserET> page = page(new Page<>(req.getPage(), req.getSize()), queryWrapper);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), BeanUtil.copyToList(page.getRecords(), PageResp.class));
    }

    @Override
    public void addUser(UserReq req) {
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserET::getUsername, req.getUsername());
        if (exists(queryWrapper)) {
            throw new AuthException("用户名已存在");
        }
        if (!save(BeanUtil.toBean(req, UserET.class))) {
            throw new AuthException("添加失败");
        }
    }

    @Override
    public void updateUser(UserReq req) {
        if (req.getUid() == null) {
            throw new AuthException("用户id不能为空");
        }
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserET::getUsername, req.getUsername())
                .ne(UserET::getUid, req.getUid());
        if (exists(queryWrapper)) {
            throw new AuthException("账号已存在");
        }
        if (!updateById(BeanUtil.toBean(req, UserET.class))) {
            throw new AuthException("修改失败");
        }
    }

    @Override
    public void delUser(Long[] ids) {
        if (!removeByIds(List.of(ids))) {
            throw new AuthException("删除失败");
        }
    }

    @Override
    public void forget(ForgetReq req) {
        LambdaQueryWrapper<UserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserET::getUsername, req.getUsername());
        queryWrapper.eq(UserET::getMail, req.getMail());
        if (!exists(queryWrapper)) {
            throw new AuthException("用户不存在");
        }
        LambdaUpdateWrapper<UserET> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserET::getUsername, req.getUsername());
        queryWrapper.eq(UserET::getPhone, req.getMail());
        updateWrapper.set(UserET::getPassword, req.getNewPassword());
        if (!update(updateWrapper)) {
            throw new AuthException("修改失败");
        }
    }
}




